<?php

namespace app\api\model;

class Product extends BaseModel
{
    protected $hidden = ['create_time','from','update_time','pivot','delete_time'];
    public static function getRecent($count){
        $products = self::limit($count)
            ->with('img')
            ->order('create_time desc')
            ->select();
        return $products;
    }
    public function images(){
        return $this->hasMany('ProductImage','product_id','id');
    }
    public function propertys(){
        return $this->hasMany('ProductProperty','product_id','id');
    }
    public function getMainImgUrlAttr($value,$data){
        $finalUrl = config('setting.img_prefix').$value;
        return $finalUrl;
    }
    public static function getDetail($id){
        $detail = self::with([
            'images'=>function($query){
                $query->with(['img'])
                    ->order('order','asc');
            }
        ])->with(['propertys'])
            ->find($id);
        return $detail;
    }
}
